package com.ldk.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ldk.admin.common.ResponseResult;
import com.ldk.base.framework.util.SystemUtil;
import com.ldk.framework.entity.AdminLog;
import com.ldk.framework.service.IAdminLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ldk.admin.common.LigerUIGridResult;
import com.ldk.base.framework.common.CustomPageable;

@Controller
@RequestMapping("/adminlog")
public class AdminLogController extends BaseController {
    /**
     * 路由地址
     */
    private final String route = "adminlog";
    /**
     * 模块名称
     */
    private final String module = "登录日志";

    @Autowired
    private IAdminLogService adminLogService;

    @RequestMapping("/list")
    //public ModelAndView list(ModelMap map){
    public ModelAndView list(ModelMap map,HttpServletRequest request,
                             HttpServletResponse response){
        //session是否过期
        this.isExpired(request,response);

        setLinks(map, "", "", module+"管理", "/"+route+"/list", "", "");

        //操作日志
        this.insertOplog(request,module+"管理","","","查看",this.getAdminUserName(request).toString(),
                Long.parseLong(this.getAdminId(request).toString()), SystemUtil.getAllSeconds());

        if (this.getRoleId(request) == 6) {
            return new ModelAndView("/"+route+"/listlog_sj");
        } else {
            return new ModelAndView("/"+route+"/listlog");
        }
    }

    @ResponseBody
    @RequestMapping("/getList")
    public LigerUIGridResult<AdminLog> getList(@RequestParam(value="page",required=false,defaultValue="1") int page,
                                           @RequestParam(value="pagesize",required=false,defaultValue="10") int pageSize,
                                           @RequestParam(value="searchKey",required=false,defaultValue="") String searchKey,
                                           HttpServletRequest request)
    {
        int limit = pageSize;
        int offset = (page -1) * pageSize;

        AdminLog query = new AdminLog();
        if(!StringUtils.isEmpty(searchKey)){
            query.setSearchKey(searchKey);
        }
        Sort sort = new Sort(Direction.DESC, "log_time");
        CustomPageable pageable = new CustomPageable(offset, limit, sort);

        long total = adminLogService.selectCount("", query);
        List<AdminLog> result = adminLogService.selectList("", query, pageable);

        LigerUIGridResult<AdminLog> grid = new LigerUIGridResult<>();
        grid.Total = total;
        grid.Rows = result;

        return grid;
    }

    @ResponseBody
    @RequestMapping("/remove/{logId}")
    public ResponseResult remove(@PathVariable long logId, HttpServletRequest request,
                                 HttpServletResponse response){
        //session是否过期
        this.isExpired(request,response);

        //System.out.println("logId="+logId);

        ResponseResult rst = new ResponseResult();
        if(logId < 1)
        {
            rst.AddError("删除登录日志错误");
            return rst;
        }
        AdminLog query = new AdminLog();
        query.setLogId(logId);

        AdminLog adminLog = adminLogService.selectByKey("", query);
        if(adminLog == null){
            rst.AddError("登录日志不存在");
            return rst;
        }

        //操作日志
        this.insertOplog(request,module,"","","删除登录日志:"+adminLog.getUserName(),this.getAdminUserName(request).toString(),
                Long.parseLong(this.getAdminId(request).toString()), SystemUtil.getAllSeconds());

        int row  = adminLogService.deleteByKey("", query);
        if(row > 0){
            rst.AddOk("删除登录日志成功");
        }
        else{
            rst.AddError("删除登录日志失败");
        }

        return rst;
    }
}
